package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

import javax.swing.*;

import registration.ClientRegistration;

@SuppressWarnings("serial")
public class ActivatePanel extends JPanel {
	private JLabel lblLink;
	private JTextField txtLink;
	private JLabel lblPassword1;
	private JLabel lblPassword2;
	private JPasswordField txtPassword1;
	private JPasswordField txtPassword2;
	private JLabel lblResult;
	private JButton btnSubmit;
	private JButton btnLogin;
	
	public ActivatePanel(){
		initializePanel();
	}

	private void initializePanel() {
		this.setLayout(null);
		
		lblLink = new JLabel("Activation Link");
		lblLink.setBounds(100, 100, 100, 30);
		lblLink.setVisible(true);
		this.add(lblLink);
		
		txtLink = new JTextField("");
		txtLink.setBounds(240 , 100 , 140 , 30);
		txtLink.setVisible(true);
		this.add(txtLink);
		
		lblPassword1 = new JLabel("Password");
		lblPassword1.setBounds(100, 150, 100, 30);
		lblPassword1.setVisible(true);
		this.add(lblPassword1);
		
		txtPassword1 = new JPasswordField("");
		txtPassword1.setBounds(240 , 150 , 140 , 30);
		txtPassword1.setVisible(true);
		this.add(txtPassword1);
		
		lblPassword2 = new JLabel("Confirm Password");
		lblPassword2.setBounds(100, 200, 120, 30);
		lblPassword2.setVisible(true);
		this.add(lblPassword2);
		
		txtPassword2 = new JPasswordField("");
		txtPassword2.setBounds(240 , 200 , 140 , 30);
		txtPassword2.setVisible(true);
		
		this.add(txtPassword2);
		
		lblResult = new JLabel("");
		lblResult.setBounds(80, 250, 500, 30);
		lblResult.setVisible(true);
		this.add(lblResult);
		
		btnSubmit = new JButton("Activate");
		btnSubmit.setBounds(120, 300, 100, 30);
		btnSubmit.setVisible(true);
		btnSubmit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				submitButton();
			}

		});
		this.add(btnSubmit);

		btnLogin = new JButton("Back to Login");
		btnLogin.setBounds(240, 300, 120, 30);
		btnLogin.setVisible(true);
		btnLogin.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				backButton();
			}

			

		});
		this.add(btnLogin);
		
	}
	
	private void backButton() {
		MainFrame.getMainFrame().remove(this);
		MainFrame.getMainFrame().addLoginPanel();
		
	}
	public String getPass(char [] cc){
		try {  
	         //--- char-array with password  
	        // char[] cc = new char[]{ 'p','a','s','s','w','o','r','d'};  
	   
	         //--- default-encoding (or any other)  
	         Charset cs = Charset.defaultCharset();  
	         CharsetEncoder enc = cs.newEncoder();  
	        
	         //--- copy into CharBuffer  
	         CharBuffer cb = CharBuffer.allocate( cc.length);  
	         cb.put( cc);  
	         cb.flip();  
	   
	         //--- create byte-repraesentation  
	         ByteBuffer bb = enc.encode( cb);  
	         byte[] ba = bb.array();  
	           
	         //--- decode (just testing)  
	         return (new String( ba));  
	         //System.out.println( "decoded: " + testString);  
	      } catch ( Exception e ) {  
	         e.printStackTrace(); 
	         return null;
	      } 
	}
	
	private void submitButton() {
		ClientRegistration clientReg = new ClientRegistration();
		String email = txtLink.getText();
		String pass1 = getPass(txtPassword1.getPassword());
		String pass2 = getPass(txtPassword2.getPassword())   ;
		if(!pass1 .equals( pass2)){
			lblResult.setText("Password don't match");
			return;
		}
		System.out.println(ClientRegistration.checkPasswordStrength(pass1));
		if(!ClientRegistration.checkPasswordStrength(pass1)){
			lblResult.setText("Password is not Strong enough. It must be 8 or more characters containing numbers");
			return;
		}
		boolean success = clientReg.activate(email , pass1);
		if(!success){
			lblResult.setText("Wrong Activation Link");
		}else{
			lblResult.setText("You are now registered. Please login with your username and password");
		}
	}
	
	
}
